<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
      label-width="80px">

      <el-row>
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="订单状态" prop="invoiceStatus">
          <el-select v-model="queryParams.invoiceStatus" placeholder="请选择订单状态" clearable size="small">
            <el-option v-for="dict in orderStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item label="所属门店"
          prop="storeId">
          <el-select v-model="queryParams.storeId"
            placeholder="请选择门店"
            clearable
            size="small"
            @keyup.enter.native="handleQuery">
            <el-option v-for="dict in storeInFoOptions"
              :key="dict.storeId"
              :label="dict.storeName"
              :value="dict.storeUid" />
          </el-select>
        </el-form-item>
        <el-form-item style="margin-left: 10px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row class="row-mbinfo">
    
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="divTable">
      <el-table border v-loading="loading" :data="rechargeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="订单ID" align="center" prop="rechargeUid" sortable /> -->
        <el-table-column label="订单号" align="center" prop="orderNo" min-width="130" :show-overflow-tooltip="true" />
        <el-table-column label="客户名称" align="center" prop="customerName" :show-overflow-tooltip="true" />
        <el-table-column label="开票金额" align="center" prop="payAmountStr" :show-overflow-tooltip="true" />
        <!-- <el-table-column label="支付金额" align="center" prop="payAmountStr" :show-overflow-tooltip="true" /> -->
        <el-table-column label="开票状态" align="center" prop="invoiceStatus" width="120" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{ scope.row.invoiceStatus == '1' ? '已开票' : '未开票' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付状态" align="center" prop="payStatus" :formatter="payStatusFormat" />
        <el-table-column label="订单来源" align="center" prop="platSource" :formatter="platSourceFormat" />
        <el-table-column label="下单时间" align="center" prop="createTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付时间" align="center" prop="payTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="未支付订单过期时间" align="center" prop="expireDate" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column> -->

        <!-- <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" /> -->
        <!-- <el-table-column label="ip地址" align="center" prop="ip" :show-overflow-tooltip="true" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">

            <el-button v-if="scope.row.invoiceStatus == '0' && scope.row.payAmountStr*1>0"  size="mini" type="text" icon="el-icon-edit" @click="handleAdd(scope.row)">开票</el-button>
            <el-button type="text" size="mini" icon="el-icon-zoom-in" @click="handleShowDetail(scope.row)">详情</el-button>

          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改充值订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
       
     
        <el-form-item label="发票号码" prop="invoiceCode">
          <el-input v-model="form.invoiceCode" placeholder="发票号码" />
        </el-form-item>

        <el-form-item label="发票图片" prop="invoiceUrl">
              <imageArraySize :imageStr.sync="form.invoiceUrl" :key="form.invoiceUrl" ref="childss"
                :msg="'图片要求：宽高等于800px，大小5M内。'" :limit="5" :size="5 * 1021 * 1024"></imageArraySize>
            </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  listInvoice,
  getRecharge,
  delRecharge,
  addInvoice,
  updateRecharge
} from "@/api/order/invoice";
import imageArraySize from "@/components/ImageUpload/imageArraySize";
import {
  selectGroup
} from "@/api/platform/store"
export default {
  name: "Recharge",
  components: {imageArraySize},
  data() {
    return {
      storeInFoOptions: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值订单表格数据
      rechargeList: [],
      // 弹出层标题
      title: "",
      //查看详细标题
      viewTitle: "",
      // 是否显示弹出层
      open: false,
      //是否显示查看详细弹出层
      viewOpen: false,
      //查看详细数据
      showData: [],
      // 订单状态字典
      orderStatusOptions: [],
      // 支付状态字典
      payStatusOptions: [],
      // 支付方式字典
      payMethodOptions: [],
      // 订单来源字典
      platSourceOptions: [],
      // 货币单位字典
      currencyUnitOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rechargeNo: null,
        customerName: null,
        phone: null,
        orderStatus: null,
        payStatus: null,
        payMethod: null,
        platSource: null,
        orderAmount: null,
        payAmount: null,
        currencyUnit: null,
        payTime: null,
        createTime: null,
        cancelTime: null,
        buyMes: null,
        expireDate: null,
        remark: null,
        ip: null,
        orderNo: null,
        invoiceStatus:null,
        storeId:null,
      },
      columns: [{
        label: '订单ID',
        dataIndex: 'rechargeUid',
      },
      {
        label: '平台ID',
        dataIndex: 'platformUid',
      },
      {
        label: '充值单号',
        dataIndex: 'rechargeNo',
      },
      {
        label: '客户ID',
        dataIndex: 'customerUid',
      },
      {
        label: '客户名称',
        dataIndex: 'customerName',
      },
      {
        label: '手机号码',
        dataIndex: 'phone',
      },
      {
        label: '订单状态',
        dataIndex: 'orderStatus',
      },
      {
        label: '支付状态',
        dataIndex: 'payStatus',
      },
      {
        label: '支付方式',
        dataIndex: 'payMethod',
      },
      {
        label: '订单来源',
        dataIndex: 'platSource',
      },
      {
        label: '订单金额',
        dataIndex: 'orderAmount',
      },
      {
        label: '支付金额',
        dataIndex: 'payAmount',
      },
      {
        label: '货币单位',
        dataIndex: 'currencyUnit',
      },
      {
        label: '支付时间',
        dataIndex: 'payTime',
      },
      {
        label: '下单时间',
        dataIndex: 'createTime',
      },
      {
        label: '订单取消时间',
        dataIndex: 'cancelTime',
      },
      {
        label: '买家留言',
        dataIndex: 'buyMes',
      },
      {
        label: '未支付订单过期时间',
        dataIndex: 'expireDate',
      },
      {
        label: '备注',
        dataIndex: 'remark',
      },
      {
        label: 'ip地址',
        dataIndex: 'ip',
      },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        invoiceCode: [{
          required: true,
          message: "发票号码不能为空",
          trigger: "blur"
        }],
      }
    };
  },
  created() {
    selectGroup().then(response => {
      this.storeInFoOptions = response.data || [];
    });
    this.getList();
    this.getDicts("order_invoice_status").then(response => {
      this.orderStatusOptions = response.data;
    });
    this.getDicts("order_pay_status").then(response => {
      this.payStatusOptions = response.data;
    });
    this.getDicts("order_pay_method").then(response => {
      this.payMethodOptions = response.data;
    });
    this.getDicts("order_plat_source").then(response => {
      this.platSourceOptions = response.data;
    });
    // this.getDicts("order_currency_unit").then(response => {
    //   this.currencyUnitOptions = response.data;
    // });
  },
  methods: {
    /** 查询充值订单列表 */
    getList() {
      this.loading = true;
      listInvoice(this.queryParams).then(response => {
        this.rechargeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 订单状态字典翻译
    orderStatusFormat(row, column) {
      return this.selectDictLabel(this.orderStatusOptions, row.orderStatus);
    },
    // 支付状态字典翻译
    payStatusFormat(row, column) {
      return this.selectDictLabel(this.payStatusOptions, row.payStatus);
    },
    // 支付方式字典翻译
    payMethodFormat(row, column) {
      return this.selectDictLabel(this.payMethodOptions, row.payMethod);
    },
    // 订单来源字典翻译
    platSourceFormat(row, column) {
      return this.selectDictLabel(this.platSourceOptions, row.platSource);
    },
    // 货币单位字典翻译
    currencyUnitFormat(row, column) {
      return this.selectDictLabel(this.currencyUnitOptions, row.currencyUnit);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rechargeUid: null,
        platformUid: null,
        rechargeNo: null,
        customerUid: null,
        customerName: null,
        phone: null,
        orderStatus: null,
        payStatus: null,
        payMethod: null,
        platSource: null,
        orderAmount: null,
        payAmount: null,
        currencyUnit: null,
        payTime: null,
        createTime: null,
        cancelTime: null,
        buyMes: null,
        expireDate: null,
        remark: null,
        ip: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rechargeUid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.form.orderUid = row.orderUid;
      this.form.invoiceId = row.invoiceUid;
      this.open = true;
      this.title = "开票";
    },
    /** 查看详情按钮操作 **/
    handleShowDetail (row) {
      const orderUid = row.orderUid;
      this.$router.push("/order/detail/" + orderUid);
    },
    handCloseDetail() {
      this.viewOpen = false
      this.showData = []
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rechargeUid = row.rechargeUid || this.ids
      getRecharge(rechargeUid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充值订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$refs.childss.selectImg();

          addInvoice(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rechargeUids = row.rechargeUid || this.ids;
      this.$confirm('是否确认删除充值订单编号为"' + rechargeUids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delRecharge(rechargeUids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/recharge/export', {
        ...this.queryParams
      }, `order_recharge.xlsx`)
    }
  }
};
</script>
